<cfscript>
component extends="SimpleDBObject" {

	function init(accessKey, secretKey) {
		var creds = _jobj("com.amazonaws.auth.BasicAWSCredentials").init(accessKey, secretKey);
		variables.sdb = _jobj("com.amazonaws.services.simpledb.AmazonSimpleDBAsyncClient").init(creds);
		variables.turnarounds = 0;
		variables.utilities =  _jobj("com.amazonaws.services.simpledb.util.SimpleDBUtils");

		super.init();
		return this;
	}
	
	function withEndpoint(endpoint) {
		variables.sdb.setEndpoint(endpoint);	   		
		return this;
	}
	
	function domain(domainName) {
		return new Domain(this, domainName);
	}
	
	function listDomains() {
		local.request = _jobj("com.amazonaws.services.simpledb.model.ListDomainsRequest");
		local.response = doSync("listDomains", local.request);
		return local.response.getDomainNames();
	}
	
	function utils() {
		return variables.utilities;
	}
	
	/* Private functions */

	package function turnarounds() {
		return variables.turnarounds;
	}

	package function do(method, args) {
		++variables.turnarounds;
		return variables.sdb[method&"Async"](args);
	}
	
	package function doSync(method, args) {
		var result = do(method, args).get();
		if (StructKeyExists(local, "result")) return local.result;
	}

}
</cfscript>
